<?php

namespace app\model;

use think\Model;

class DailyDeliveryData extends Model
{
    // 设置当前模型对应的数据表
    protected $name = 'daily_delivery_data';
    // 设置当前模型主键
    protected $pk = 'id';

    // 根据条件获取数据
    public function getDataByWhere($where = [], $fields = 'id')
    {
        $data = $this::where($where)->field($fields)->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 获取数据
     * 
     * @dateTime 2022/09/03
     * @author zhixiang0215@126.com
     * @param array where
     * @param int page
     * @param int limit
     * @param string fields
     * @param string order
     * @return array
     */
    public function getPagingDataByWhere(array $where, int $page = 1, int $limit = 10, string $fields = 'id', string $order = 'date desc')
    {
        $offset = ($page - 1) * $limit;
        if ($offset < 0) $offset = 0;

        $total = $this::where($where)->count();

        if ($total == 0) return ['total' => 0, 'total_page' => 0, 'data' => []];
        $totalPage = ceil($total / $limit);

        $data = $this::where($where)->field($fields)->order($order)->limit($offset, $limit)->select()->toArray();

        return ['total' => $total, 'total_page' => $totalPage, 'data' => $data];
    }
}